package com.company.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.utils.RestUtil;
import com.company.project.pojo.Rest;
import com.company.project.pojo.ServiceCode;
import com.company.project.common.utils.JwtUtil;
import com.company.project.pojo.institution.Institution;
import com.company.project.pojo.institution.InstitutionDTO;
import com.company.project.pojo.institution.InstitutionSelectVO;
import com.company.project.pojo.roleVillage.RoleVillage;
import com.company.project.pojo.user.User;
import com.company.project.service.IInstitutionService;
import com.company.project.service.IRoleVillageService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 单位 前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2019-08-09
 */
@Api(value = "单位",tags = "单位")
@Controller
@RequestMapping("institution")
public class InstitutionController {

    @Autowired
    HttpServletRequest request;
    @Autowired
    private IInstitutionService iInstitutionService;
    @Autowired
    private IRoleVillageService iRoleVillageService;

    @ApiOperation(value = "获取单位列表分页")
    @RequestMapping(value = "getInstitutionListPage", method = RequestMethod.GET)
    @ResponseBody
    public Rest getInstitutionListPage(Integer currentPage, Integer contradictionId){
        IPage<InstitutionDTO> page = iInstitutionService.getInstitutionListPage(new Page(currentPage, 10), contradictionId);

        return RestUtil.rest(ServiceCode.CODE_200, "成功" , page);
    }

    @ApiOperation(value = "获取单位列表")
    @RequestMapping(value = "getInstitutionList", method = RequestMethod.GET)
    @ResponseBody
    public Rest getInstitutionList(){
        QueryWrapper<Institution> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);

        List institutionList = iInstitutionService.list(queryWrapper);

        return RestUtil.rest(ServiceCode.CODE_200, "成功" , institutionList);
    }

    @ApiOperation(value = "获取单位")
    @RequestMapping(value = "getInstitution", method = RequestMethod.GET)
    @ResponseBody
    public Rest getInstitution(int id){
        QueryWrapper<Institution> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);

        Institution institution = iInstitutionService.getOne(queryWrapper);

        return RestUtil.rest(ServiceCode.CODE_200, "成功" , institution);
    }

    @ApiOperation(value = "根据角色获取单位列表")
    @ApiImplicitParam(paramType="header", name = "token", value = "token", required = true, dataType = "String")
    @RequestMapping(value = "getInstitutionListByRoleId", method = RequestMethod.GET)
    @ResponseBody
    public Rest<InstitutionSelectVO> getInstitutionListByRoleId(){
        Claims claims = JwtUtil.parseJWT(request.getHeader("token"));
        Integer role_id = claims.get("role_id", Integer.class);

        List<InstitutionDTO> institutionDTOList = iInstitutionService.getInstitutionListByRoleId(role_id);

        List<InstitutionSelectVO> institutionSelectVOList = new ArrayList<>();
        for (InstitutionDTO institutionDTO: institutionDTOList){
            InstitutionSelectVO institutionSelectVO = new InstitutionSelectVO();
            BeanUtils.copyProperties(institutionDTO, institutionSelectVO);

            institutionSelectVOList.add(institutionSelectVO);
        }

        return RestUtil.rest(ServiceCode.CODE_200, "成功" , institutionSelectVOList);
    }

    @ApiOperation(value = "根据类型获取单位列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", value = "单位类型", name = "type", required = true, dataType = "int"),
    })
    @RequestMapping(value = "getInstitutionListByType", method = RequestMethod.GET)
    @ResponseBody
    public Rest<InstitutionSelectVO> getInstitutionListByType(Integer type){
        List<InstitutionDTO> institutionDTOList = iInstitutionService.getInstitutionListByType(type);

        List<InstitutionSelectVO> institutionSelectVOList = new ArrayList<>();
        for (InstitutionDTO institutionDTO: institutionDTOList){
            InstitutionSelectVO institutionSelectVO = new InstitutionSelectVO();
            BeanUtils.copyProperties(institutionDTO, institutionSelectVO);

            institutionSelectVOList.add(institutionSelectVO);
        }

        return RestUtil.rest(ServiceCode.CODE_200, "成功" , institutionSelectVOList);
    }

    @ApiOperation(value = "添加单位")
    @RequestMapping(value = "addInstitution", method = RequestMethod.POST)
    @ResponseBody
    public Rest addInstitution(@RequestBody Institution institution){
        QueryWrapper<Institution> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", institution.getName());
        queryWrapper.eq("is_delete", 0);
        List<Institution> institutionList = iInstitutionService.list(queryWrapper);
        if(institutionList.size() > 0){
            return RestUtil.rest(ServiceCode.CODE_403, "该单位已存在");
        }

        Claims claims = JwtUtil.parseJWT(request.getHeader("token"));
        Integer user_id = claims.get("user_id", Integer.class);

        institution.setInputUserId(user_id);
        iInstitutionService.save(institution);

        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！");
    }

    @ApiOperation(value = "修改单位")
    @RequestMapping(value = "updateInstitution", method = RequestMethod.POST)
    @ResponseBody
    public Rest updateInstitution(@RequestBody Institution institution){
        QueryWrapper<Institution> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", institution.getName());
        queryWrapper.eq("is_delete", 0);
        List<Institution> institutionList = iInstitutionService.list(queryWrapper);
        for (int i=0; i<institutionList.size(); i++){
            if (institutionList.get(i).getId().equals(institution.getId())){
                continue;
            }
            return RestUtil.rest(ServiceCode.CODE_403, "该单位已存在");
        }

        iInstitutionService.updateById(institution);

        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！");
    }

    @ApiOperation(value = "删除单位")
    @RequestMapping(value = "deleteInstitution", method = RequestMethod.POST)
    @ResponseBody
    public Rest deleteInstitution(Integer id){
        Institution institution = new Institution();
        institution.setId(id);
        institution.setIsDelete(1);

        iInstitutionService.updateById(institution);

        return RestUtil.rest(ServiceCode.CODE_200, "成功");
    }

}

